﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Acms.AcmsControls.ContentControls.FrontEnd
{
    /// <summary>
    /// Renders hyper link to currnet / containing post.
    /// </summary>
    public class PostLink : BasePostPropertyControl
    {
        public override void DataBind()
        {
            if(!string.IsNullOrEmpty(Text))
            {
                TextToUse = Text;
            }
            if (!string.IsNullOrEmpty(ToolTip))
            {
                ToolTipToUse = ToolTip;
            }
            var post = FindCurrentPost();
            if (post != null)
            {
                if (string.IsNullOrEmpty(TextToUse))
                {
                    TextToUse = post.DisplayName;
                }
                if (string.IsNullOrEmpty(ToolTipToUse))
                {
                    ToolTipToUse = post.DisplayName;
                }

                if (string.IsNullOrEmpty(TemplateName))
                {
                    Url = post.Url;
                }
                else
                {
                    Url = post.GetUrl(TemplateName);
                }
            }
            else
            {
                if (string.IsNullOrEmpty(TextToUse))
                {
                    TextToUse = "Display Name Here";
                }
                if (string.IsNullOrEmpty(ToolTipToUse))
                {
                    ToolTipToUse = "Display Name As Title";
                }
                if (string.IsNullOrEmpty(TemplateName))
                {
                    Url = "#";
                }
                else
                {
                    Url = string.Concat("#?Template=",TemplateName);
                }
                
            }

            base.DataBind();
        }

        protected string TextToUse { get; set; }
        protected string ToolTipToUse { get; set; }
        /// <summary>
        /// Text to display - leave blank for the Display Name of the post
        /// </summary>
        public string Text { get; set; }
        /// <summary>
        /// Url for the post
        /// </summary>
        public string Url { get; set; }
        /// <summary>
        /// Css class - will render on 'a' tag
        /// </summary>
        public string CssClass { get; set; }
        /// <summary>
        /// Tool tip to display - lkeave blank for the display name of the post
        /// </summary>
        public string ToolTip { get; set; }
        /// <summary>
        /// Adds relevent quesrysting parameters to link to given tamplate name
        /// </summary>
        public string TemplateName { get; set; }

        protected override object SaveViewState()
        {
            ViewState["TextToUse"] = TextToUse;
            ViewState["Url"] = Url;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            TextToUse = ViewState["TextToUse"] as string;
            Url = ViewState["Url"] as string;
        }

        protected virtual void RenderInnerContents(System.Web.UI.HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Span);
            writer.Write(TextToUse);
            writer.RenderEndTag();
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (!string.IsNullOrEmpty(CssClass))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
            }
            if (!string.IsNullOrEmpty(ToolTipToUse))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Title, ToolTipToUse);
            }
            
            writer.AddAttribute(HtmlTextWriterAttribute.Href, Page.ResolveUrl(Url));
                writer.RenderBeginTag(HtmlTextWriterTag.A);
                RenderInnerContents(writer);
            writer.RenderEndTag();
        }

    }
}
